class WordsController < ApplicationController
  before_filter :check_creator_authentication, :only => ["update", "edit", "destroy"]

  def check_creator_authentication 
    unless session[:user].to_s == Word.find(params[:id]).creator_id.to_s
      session[:intended_params] = params      
      redirect_to :controller => "admin", :action => "login"
    end 
  end 

  
  # GET /words
  # GET /words.xml
  def index
    @words = Word.search(params[:search],params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @words }
    end
  end

  # GET /words/1
  # GET /words/1.xml
  def show
    @word = Word.find(params[:id])
    @comments = WordComment.paginate :per_page => 5, :page => params[:page], :conditions => ['word_id == ?', params[:id]]

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @words }
    end
  end

  # GET /words/new
  # GET /words/new.xml
  def new
    @word = Word.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @words }
    end
  end

  # GET /words/1/edit
  def edit
    @word = Word.find(params[:id])
  end

  # POST /words
  # POST /words.xml
  def create
    @word = Word.new(params[:word])
    @word.creator_id = session[:user]
    respond_to do |format|
      if @word.save
        flash[:notice] = 'Word was successfully created.'
        format.html { redirect_to(@word) }
        format.xml  { render :xml => @word, :status => :created, :location => @word }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @word.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /words/1
  # PUT /words/1.xml
  def update
    @word = Word.find(params[:id])

    respond_to do |format|
      if @word.update_attributes(params[:word])
        flash[:notice] = 'Word was successfully updated.'
        format.html { redirect_to(@word) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @word.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /words/1
  # DELETE /words/1.xml
  def destroy
    @word = Word.find(params[:id])
    @word.destroy

    respond_to do |format|
      format.html { redirect_to(words_url) }
      format.xml  { head :ok }
    end
  end
  
  def bookmark
    #logger.info "Request URL is " + request.url
    @word = Word.find(params[:id])
    @bookmark = Bookmark.find_by_user_id_and_word_id(session[:user], @word.id )
    if @bookmark
      @bookmark.rating += 1 #everytime add rating 1
    else  
      @bookmark = Bookmark.new
      @bookmark.word = @word
      @bookmark.user_id = session[:user]
      @bookmark.rating = 5 # start from rating 5
    end
    @bookmark.save!
    render :update do |page|
      page.replace_html("word#{@word.id}", :partial => "word", :object => @word) 
    end
  end
  
  #considering move it to bookmark controller or bookmark model?
  def reduce_rating
    @word = Word.find(params[:id])
    @bookmark = Bookmark.find_by_user_id_and_word_id(session[:user], @word.id )
    if @bookmark
      @bookmark.rating -= 1
      if @bookmark.rating <= 0
        @bookmark.destroy
      else
        @bookmark.save!
      end
      render :update do |page|
        page.replace_html("word#{@word.id}", :partial => "word", :object => @word) 
      end
    else
      #TODO if not exist, do some flash update work in the page
      flash[:notice] = "该单词不在您的学习列表中"
    end
  end
  
  
  def remove_bookmark
      @word = Word.find(params[:id])
      @bookmark = Bookmark.find_by_user_id_and_word_id(session[:user], @word.id )
      if @bookmark
        @bookmark.destroy
        render :update do |page|
          page.hide "word#{@word.id}"
        end
      else
        #TODO update flash if not exist
        flash[:notice] = "该单词不在您的学习列表中"
      end
  end
  
  def bookmarked
    @user = User.find(session[:user])
    @words = @user.learning_words.paginate :per_page =>5, :page => params[:page]
    render :action => "index" 
  end
    
end
